package com.zzyl.common.ai;

import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.ChatCompletion;
import com.openai.models.ChatCompletionCreateParams;
import com.openai.models.ResponseFormatJsonObject;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Component
@Slf4j
@AllArgsConstructor
public class AiModelInvoker {
    private final BaiLianAIProperties baiLianAIProperties;

    public String invoke(String prompt) {


        OpenAIClient client = OpenAIOkHttpClient.builder()

                .apiKey(baiLianAIProperties.getApiKey())
                .baseUrl(baiLianAIProperties.getUrl())
                .build();

        ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
                .addUserMessage(prompt)
                .responseFormat(ResponseFormatJsonObject.builder().build())
                .model(baiLianAIProperties.getModelName())
                .build();


            ChatCompletion chatCompletion = client.chat().completions().create(params);
            String result = chatCompletion.choices().get(0).message().content().get();
            log.info("result:{}", result);
            return result;

    }
}
